<?php
!defined('QAPP') AND exit('Forbidden');
$extension = @get_loaded_extensions();
if ($extension == false) {
    $extension = array();
}

/**
 * 友好的输出调试信息
 * @param mixed $var
 * @param bool $die
 */
function dumpForInstall($var, $die = false)
{
    echo '<pre>';

    var_dump($var);

    echo '</pre>';

    if ($die) die;
}
function base62ForInstall($x)
{
    $show = '';
    while ($x > 0) {
        $s = $x % 62;
        if ($s > 35) {
            $s = chr($s + 61);
        } elseif ($s > 9 && $s <= 35) {
            $s = chr($s + 55);
        }
        $show = $s . $show;
        $x = floor($x / 62);
    }
    if (!$show)
        $show = "0";
    return $show;
}
/**
 * 获取CUID（短一些的GUID）
 * @return string
 */
function getCuidForInstall()
{
    @list($usec, $sec) = explode(" ", microtime());
    //$sec = substr($sec, 0, 6);
    $sec_a = substr($sec, 2, 3) + rand(100, 999);
    $sec_b = substr($sec, 5, 5) + rand(100, 999);
    $str_sec = base62ForInstall($sec_a) . base62ForInstall($sec_b);
    $str_usec = base62ForInstall(($usec + rand(1, 9)) * 1000000);
    $left = $str_sec . $str_usec . base62ForInstall(rand(1000, 9999));

    if (strlen($left) < 12) {
        $left .= base62ForInstall(rand(0, 9));
    }

    return strtoupper($left);
}


function ext_exist($extname)
{
    global $extension;
    if (in_array($extname, $extension)) {
        return true;
    } else {
        return false;
    }
}

function dir_test($dir)
{
    $dir = $dir . '/rwtest/';
    if (is_dir($dir)) {

    } else {
        @mkdir($dir);
        if (!is_dir($dir)) {
            Return false;
        }
    }
    $filename = $dir . 'test.txt';
    $fp = @fopen($filename, "w");
    if (!@fwrite($fp, "readwritetest")) {
        @fclose($fp);
        rmdir($dir);
        Return false;
    } else {
        @fclose($fp);
        unlink($filename);
    }
    rmdir($dir);
    Return true;
}


/* * 获取GET参数
 * @param $a
 * @return null
 */
function getGet($a, $def = null)
{
    if (isset($_GET[$a])) {
        return $_GET[$a];
    } else {
        return $def;
    }
}

/* * 获取POST参数
 * @param $a
 * @return null
 */
function getPost($a, $def = null)
{
    if (isset($_POST[$a])) {
        return $_POST[$a];
    } else {
        return $def;
    }
}


/* * 获取Session
 * @param $a
 * @return string| null
 */
function session_get($a, $def = null)
{
    if (!isset($_SESSION)) {
        @session_start();
    }

    if (isset($_SESSION[$a])) {
        return $_SESSION[$a];
    } else {
        return $def;
    }
}

/* * 设置Session
 * @param $a Session的名称
 * @param $v
 * @return null
 */
function session_set($a, $v)
{
    if (!isset($_SESSION)) {
        @session_start();
    }

    $_SESSION[$a] = $v;
}

/**
 * 注销当前用户的session
 */
function session_clear()
{
    if (!isset($_SESSION)) {
        @session_start();
    }
    @session_destroy();
}

function array_value($arr, $key, $default = '')
{
    return isset($arr[$key]) ? $arr[$key] : $default;
}


/**
 * 字节格式化 把字节数格式为 B K M G T 描述的大小
 * @return string
 */
function byte_format($size, $dec = 2)
{
    $unit = array("B", "KB", "MB", "GB", "TB", "PB");
    $pos = 0;
    while ($size >= 1024) {
        $size /= 1024;
        $pos++;
    }
    return round($size, $dec) . " " . $unit[$pos];
}


function real_size_byte($size = 0)
{
    if (!$size) {
        return 0;
    }

    $scan['gb'] = 1073741824; //1024 * 1024 * 1024;
    $scan['g'] = 1073741824; //1024 * 1024 * 1024;
    $scan['mb'] = 1048576;
    $scan['m'] = 1048576;
    $scan['kb'] = 1024;
    $scan['k'] = 1024;
    $scan['b'] = 1;

    foreach ($scan as $unit => $factor) {
        if (strlen($size) > strlen($unit)
            && strtolower(substr($size, strlen($size) - strlen($unit))) == $unit) {
            return substr($size, 0, strlen($size) - strlen($unit)) * $factor;
        }
    }

    return $size;
}

function tableex($table = '')
{
    if (substr($table, 0, strlen(TableEx)) == TableEx) {
        return $table;
    } else {
        return TableEx . $table;
    }
}